<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="jquery-3.3.1.min.js"></script>
    <style>
        * {
            padding: 0;
            margin: 0;
        }
        
        .wrap {
            width: 300px;
            margin: 100px auto 0;
        }
        
        table {
            border-collapse: collapse;
            border-spacing: 0;
            border: 1px solid #c0c0c0;
            width: 300px;
            margin: 0 auto;
        }
        
        th,
        td {
            border: 1px solid #d0d0d0;
            color: #404060;
            padding: 10px;
        }
        
        th {
            background-color: #09c;
            font: bold 16px "微软雅黑";
            color: #fff;
        }
        
        td {
            font: 14px "微软雅黑";
        }
        
        tbody tr {
            background-color: #f0f0f0;
        }
        
        tbody tr:hover {
            cursor: pointer;
            background-color: #fafafa;
        }
        
        .dd {
            width: 70px;
            height: 40px;
            text-align: center;
        }
    </style>
</head>

<body>
    <div style="margin: 0 auto; width: 300px;">
        <input type="text" id="sname">
        <input type="text" id="price">
        <input type="button" id="add" value="添加" class="dd">
        <input type="button" id="dele" value="删除" class="dd">
        <input type="button" id="fanxuan" value="反选" class="dd">
    </div>

    <table>
        <thead>
            <th><input type="checkbox" id="check_one" /></th>
            <th>商品</th>
            <th>价钱</th>
        </thead>
        <tbody id="tbody">
            <tr>
                <td><input type="checkbox" /></td>
                <td>ipnone8</td>
                <td>8000</td>
            </tr>
            <tr>
                <td><input type="checkbox" /></td>
                <td>ipad Pro</td>
                <td>5000</td>
            </tr>
            <tr>
                <td><input type="checkbox" /></td>
                <td>ipad Air</td>
                <td>2000</td>
            </tr>
            <tr>
                <td><input type="checkbox" /></td>
                <td>Apple Watch</td>
                <td>2000</td>
            </tr>
        </tbody>
    </table>
    <script>
        var al_check = document.getElementById("tbody").getElementsByTagName("input");
        $("#check_one").click(function() {
            console.log(this.checked);
            for (var i = 0; i < al_check.length; i++) {
                al_check[i].checked = this.checked;
            }
        });
        $("#fanxuan").click(function() {
            console.log(this.checked);
            for (var i = 0; i < al_check.length; i++) {
                al_check[i].checked = !al_check[i].checked;
            }
        });
        $("#add").click(function() {
            var tr_in = document.createElement("tr");
            var tbody = document.getElementById("tbody");
            var sname = $("#sname").val();
            console.log(sname);
            var price = $("#price").val();
            tbody.appendChild(tr_in);
            var td3 = document.createElement("td");
            td3.innerHTML = '<input type="checkbox" />';
            var  td1  =  document.createElement('td');
            td1.innerHTML = sname;
            var  td2  =  document.createElement('td');
            td2.innerHTML = price;
            tr_in.appendChild(td3);
            tr_in.appendChild(td1);
            tr_in.appendChild(td2);
        });
        $("#dele").click(function() {
            $("input[type='checkbox']:checked").each(function() {
                n = $(this).parents("tr").index();
                $("tbody#tbody").find("tr:eq(" + n + ")").remove();
            });
        });
    </script>
</body>

</html>